package com.ydgk.servlet;

import com.alibaba.fastjson.JSON;
import com.ydgk.bean.Type;
import com.ydgk.bean.User;
import com.ydgk.dao.UserDao;
import com.ydgk.service.TypeService;
import com.ydgk.service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.print.Book;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UserServlet", urlPatterns = "/UserServlet.action")
public class UserServlet extends BaseServlet {

    protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map map=request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        user.setUbrith(new Date(System.currentTimeMillis()));
        System.out.println(user);
        UserService userService = new UserService();
        int i=userService.save(user);
        String b=null;
        if(i>0){
            b="true";
        }else{
            b="false";
        }
        response.getWriter().print(b);
    }

    protected void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int uid= Integer.parseInt(request.getParameter("uid"));
        int utype= Integer.parseInt(request.getParameter("utype"));
        //String uadress=request.getParameter("uadress");
        int uphone= Integer.parseInt(request.getParameter("uphone"));
        User user=new User();
        user.setUid(uid);
        user.setUtype(utype);
        //user.setUadress(uadress);
        user.setUphone(uphone);
        List list=new ArrayList();
        int i=new UserService().updateById(user);
        HashMap map = new HashMap();
        if(i>0){
            map.put("flag","true");
        }else{
            map.put("flag","false");
        }
        response.getWriter().print(JSON.toJSONString(map));
    }

    protected void typeFindAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TypeService typeService = new TypeService();
        List<Type> typeList=typeService.findAll();
        int i= Integer.parseInt(request.getParameter("uid"));
        User user = new UserService().findById(i);
        HashMap map=new HashMap();
        map.put("user",user);
        map.put("typeList",typeList);
        response.getWriter().print(JSON.toJSONString(map));
    }

    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = new UserService();
        List<User> userList = userService.findAll();
        request.setAttribute("userList",userList);
        request.setAttribute("mainPage","userFindAll.jsp");
        request.getRequestDispatcher("index.jsp").include(request,response);
    }

    protected void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int uid= Integer.parseInt(request.getParameter("uid"));
        UserService userService=new UserService();
        int i=userService.deleteById(uid);
        HashMap map = new HashMap();
        if(i>0){
            System.out.println("suceess");
            map.put("flag","true");
        }else{
            System.out.println("defaled");
            map.put("flag","false");
        }
        response.getWriter().print(JSON.toJSONString(map));
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
            String code = request.getParameter("imageCode");
            HttpSession session = request.getSession();
            if (!code.equals(session.getAttribute("sRand"))) {
                request.setAttribute("error", "验证码错误");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }

            User user = new User();
            user.setUname(request.getParameter("nickName"));
            user.setUpass(request.getParameter("password"));
            UserService userService = new UserService();
            user = userService.login(user);

            if (user == null) {
                request.setAttribute("error", "用户名或者密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            } else {
                session.setAttribute("user", user);
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            }
    }

    protected void findUserAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        UserService userService = new UserService();
        int maxPage = userService.maxPage("user");
        String pn = request.getParameter("pageNumber");
        String pc = request.getParameter("pageCount");
        int pageNumber = 1;
        int pageCount = 8 ;
        try {
            pageNumber = Integer.parseInt(pn);
            pageCount = Integer.parseInt(pc);
        }catch (NumberFormatException e){
            pageNumber = 1;
            pageCount = 8 ;
        }
        UserDao userDao = new UserDao();
        userDao.pageCount = pageCount;
        if(pageNumber <1){
            pageNumber=1;
        }

        if(pageNumber > maxPage){
            pageNumber = maxPage ;
        }

        List<User> userList = userService.paging(pageNumber);
        int total = userService.total();
        HashMap map = new HashMap() ;

        map.put("code",0);
        map.put("msg","");
        map.put("data",userList);
        map.put("pageCount",pageCount);
        map.put("pageNumber",pageNumber);
        map.put("total",total);

        String str = JSON.toJSONString(map);

        response.getWriter().print(str);

    }

}
